package com.tmp.alarm.task;

import com.tmp.alarm.utils.MySQLUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.ResultSet;

/**
 * 运行在不同队列中处于等待的状态的job数告警
 *
 */
public class PendingJobAlarm {

    private  final  static Logger LOGGER = LoggerFactory.getLogger(PendingJobAlarm.class);

    /**
     * 计算针对不同的队列中的job数,判断等待中的job数是否超过配置中指定的数,超过则告警
     */
    public  static  void pendingJobAlarm(){

        String sql = "SELECT max(m.id),m.type,m.queue_name,m.active_job_num,m.pending_job_num FROM mart_mobile_queue_info AS m WHERE type=1 GROUP BY queue_name ORDER BY id DESC";

        try {
            Connection connection = MySQLUtils.getConnection();
            ResultSet rs = MySQLUtils.query(connection,sql);
            rs.next();
            int rows = rs.getRow();
            if(rows>0){
                rs.beforeFirst();
                while (rs.next()){

                    String queue_name = rs.getString("queue_name");
                    String  active_job_num = rs.getString("active_job_num");
                    String  pending_job_num = rs.getString("pending_job_num");
                    dealPendingJobs(queue_name,active_job_num,pending_job_num);

                }

            }else{
                LOGGER.info("查询等待job暂时无满足条件数据...sql==="+sql);
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("等待job查询异常..."+e.getMessage());
        }
    }

    private static void dealPendingJobs(String queue_name, String active_job_num, String pending_job_num) {

    }

}




